import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 62536
 * Date: 2024-03-10
 * Time: 16:07
 */
public class Test {

    public static void main(String[] args) {
        int[] arr = {0,1,0,3,12};
        moveZeroes(arr);



        for (int i : arr) {
            System.out.print(i+ " ");
        }
        System.out.println();
    }

    //移动零
    //给定一个数组 nums，编写一个函数将所有 0 移动到数组的末尾，同时保持非零元素的相对顺序
    //输入: nums = [0,1,0,3,12]
    //输出: [1,3,12,0,0]
    public static void moveZeroes(int[] nums) {
        int p1 = 0;
        int p2 = 1;

        if(nums.length == 1) {
            return;
        }

        while(p2 < nums.length) {
            while(nums[p1] != 0) {
                p1++;
                p2++;
                if(p2 >= nums.length){
                    return;
                }
            }

            while(nums[p2] == 0) {
                p2++;
                if(p2 >= nums.length){
                    return;
                }
            }

            if(nums[p1] == 0 && nums[p2] != 0) {
                swap(nums,p1,p2);
            }
        }


    }

    private static void swap(int[] array,int i,int j) {
        int tmp = array[j];
        array[j] = array[i];
        array[i] = tmp;
    }


    //最长连续序列
    //输入：nums = [100,4,200,1,3,2]
    //输出：4
    //解释：最长数字连续序列是 [1, 2, 3, 4]。它的长度为 4。
    public static int longestConsecutive(int[] nums) {

        Set<Integer> set = new HashSet<>();

        for (int i : nums) {
            set.add(i);
        }

        int longestNum = 0;

        for(int num : set){
            if(!set.contains(num - 1)){
                int currentLongestNum = 1;
                int currentNum = num;

                while(set.contains(currentNum + 1)){
                    currentLongestNum++;
                    currentNum += 1;
                }

                longestNum = Math.max(currentLongestNum, longestNum);
            }
        }

        return longestNum;




    }
}
